package com.xyoto.community.controller;

import com.xyoto.community.aspect.annotation.RequiresLogin;
import com.xyoto.community.dto.CheckContentDTO;
import com.xyoto.community.dto.PageDTO;
import com.xyoto.community.dto.SendPicturePrivateLetterDTO;
import com.xyoto.community.dto.SendTextPrivateLetterDTO;
import com.xyoto.community.service.PictureService;
import com.xyoto.community.service.PrivateLetterService;
import com.xyoto.community.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

/**
 * 私信
 */
@Api(tags = "私信")
@RestController
@RequestMapping("/privateLetter")
public class PrivateLetterController {

    @Autowired
    private PrivateLetterService privateLetterService;

    @Autowired
    private PictureService pictureService;

    @RequiresLogin
    @ApiOperation("发送文本私信内容")
    @PostMapping("/sendText")
    public Result sendPrivateLetter(@RequestBody @Valid SendTextPrivateLetterDTO sendTextPrivateLetterDTO){
        // 校验文本内容
        pictureService.checkContent(new CheckContentDTO(sendTextPrivateLetterDTO.getContent()));
        return privateLetterService.sendText(sendTextPrivateLetterDTO);
    }

    @RequiresLogin
    @ApiOperation("发送私信图片")
    @PostMapping("/sendPicture")
    public Result sendPicture(MultipartFile multipartFile
            , SendPicturePrivateLetterDTO sendPicturePrivateLetterDTO){
        return privateLetterService.sendPicture(multipartFile,sendPicturePrivateLetterDTO);
    }

    @RequiresLogin
    @ApiOperation("获取私信内容")
    @GetMapping("/getPrivateLetter")
    public Result getPrivateLetter(@RequestParam("targetUserId") Integer targetUserId,
                                   @Valid PageDTO pageDTO){
        return privateLetterService.getPrivateLetter(targetUserId,pageDTO);
    }

    @RequiresLogin
    @ApiOperation("删除私信列表")
    @DeleteMapping("/removePrivateLetterList")
    public Result removePrivateLetterList(@RequestParam("privateLetterListId") Integer privateLetterListId){
        return privateLetterService.removePrivateLetterList(privateLetterListId);
    }

}
